/*
 * Linear Array 1D benchmark patient.  (healthy)
 */

#include <stdio.h>
#include <stdlib.h>
#include "common.h"

#define SIZE 10	// default size
#define FILTERWIDTH 10

int kernel(int size, double** inMatrix1, double** inMatrix2, double** outMatrix)
{
#ifdef DEBUG
	printf("Kernel...\n");
#endif
	int i, j, k;

	// iterate over the rows of inMatrix1
	for(i = 0 ; i < size ; i++)
	{
		// iterate over the columns of inMatrix2
		for(j = 0 ; j < size ; j++)
		{
			// iterate over the columns of inMatrix1, rows of inMatrix2
			for (k = 0; k < size; k++) 
			{
				outMatrix[i][j] += inMatrix1[i][k] * inMatrix2[k][j];
			}
		}
	}

#ifdef DEBUG
	printf("Kernel done!\n");
#endif
}

int main( int argc, char* argv[] )
{
	int size = parseSize(argc, argv);
	int iterations = parseIterations(argc, argv);
	int filterwidth = FILTERWIDTH;
	int i, j, index;
	int seed = 10000;

	// declare data structures DYNAMICALLY!
	double** inMatrix1 = malloc(sizeof(double*) * size);
	double** inMatrix2 = malloc(sizeof(double*) * size);
	double** outMatrix = malloc(sizeof(double*) * size);

#ifdef DEBUG
	printf( "\nRunning %s with size: %d\n\n", argv[0], size );	
#endif

	// initialize the random generator, so we have consistent results
	srand(seed);

	// initialize data structures
#ifdef DEBUG
	printf("Initializing Matrices\n");
#endif
	for (i = 0 ; i < size ; i++) {
		inMatrix1[i] = malloc(sizeof(double) * size);
		inMatrix2[i] = malloc(sizeof(double) * size);
		outMatrix[i] = malloc(sizeof(double) * size);
		for (j = 0 ; j < size ; j++) {
			inMatrix1[i][j] = 100.0 * ((double)rand() / ((double)(RAND_MAX)+(double)(1)));
			inMatrix2[i][j] = 100.0 * ((double)rand() / ((double)(RAND_MAX)+(double)(1)));
			outMatrix[i][j] = 0.0;
		}
	}

#ifdef DEBUG
	printf("Running...\n");
#endif
	char sizeString[256];
	sprintf(sizeString, "%d", size);
   	kernel(size, inMatrix1, inMatrix2, outMatrix);
   	kernel(size, inMatrix1, inMatrix2, outMatrix);
    for(i=0; i <  iterations; i++) {
    	kernel(size, inMatrix1, inMatrix2, outMatrix);
    }
	return 0;
}

